﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using AjaxControlToolkit;

namespace ExtendControls
{
    /// <summary>
    /// Summary description for NumericTextBox
    /// </summary>

    [ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")]
    public class NumericTextBox : TextBox
    {
        #region PRIVATE
        //properties IsRequired
        private bool _IsRequired = false;
        private string _RequidMessage = "*";
        //properties AcceptNegativ
        private bool _AcceptNegativ = true;

        //properties Interval
        private Int32 _MinValue = Int32.MinValue;
        private Int32 _MaxValue = Int32.MaxValue;

        private string _ClientScript = "true";
        private string _ValidChars = "+123456789";
        //controls
        private RequiredFieldValidator req;
        private FilteredTextBoxExtender filter;
        #endregion PRIVATE

        #region PUBLIC
        public bool IsRequired
        {
            get { return _IsRequired; }
            set { _IsRequired = value; }
        }
        public string RequidMessage
        {
            get { return _RequidMessage; }
            set { _RequidMessage = value; }
        }
        public bool AcceptNegativ
        {
            get { return _AcceptNegativ; }
            set { _AcceptNegativ = value; }
        }
        public Int32 MinValue
        {
            get { return _MinValue; }
            set { _MinValue = value; }
        }
        public Int32 MaxValue
        {
            get { return _MaxValue; }
            set { _MaxValue = value; }
        }
        public string ClientScript
        {
            get { return _ClientScript; }
            set { _ClientScript = value; }
        }
        #endregion PUBLIC

        protected override void OnInit(EventArgs e)
        {            
            base.OnInit(e);
            // AddControls();
        }
      
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            AddControls();
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            //check _IsRequired
            if (_IsRequired)
            {
                req.RenderControl(writer);
            }

            filter.RenderControl(writer);
        }

        public NumericTextBox()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private void AddControls()
        {
            this.BackColor = System.Drawing.Color.White;

            //properties IsRequired
            if (_IsRequired)
            {
                req = new RequiredFieldValidator();
                req.ControlToValidate = this.ID;
                req.ErrorMessage = this.RequidMessage;
                req.Display = ValidatorDisplay.Dynamic;
                req.EnableClientScript = (this.ClientScript.ToLower() != "false");
                Controls.Add(req);
                //set backgorund color
                this.BackColor = System.Drawing.ColorTranslator.FromHtml(Properties.Settings.Default.RequidColor);
            }

            //properties ReadOnly
            if (this.ReadOnly)
            {
                //set backgorund color
                this.BackColor = System.Drawing.ColorTranslator.FromHtml(Properties.Settings.Default.ReadOnlyColor);
            }

            //properties AcceptNegativ
            if (_AcceptNegativ)
                _ValidChars += "-";

            //The Filter
            filter = new FilteredTextBoxExtender();
            filter.TargetControlID = this.ID;
            filter.FilterType = FilterTypes.Custom;
            filter.ValidChars = _ValidChars;
            Controls.Add(filter);
        }

    }

}
